Core.Profiler = function() {
	this.results = [];
	this.collective = false;	
};
	
Core.Profiler.prototype = {

	/**
	 * Starts profiler's collective mode.
	 * Profiler will now collect measures for the same labels.
	 */
	startCollective: function() {
		this.collective = true;
	},
	
	/**
	 * Stops profiler's collective mode.
	 * Profiler will flush all collected measures into the console.
	 */
	stopCollective: function() {
		this.collective = false;
		
		Object.each(this.results, function(entry, label) {
			if(entry) {
				console.log(label, ", Elapsed", entry.collected, "ms", ", Count", 
						entry.count, ", Average", entry.collected / entry.count, "ms");
			}
		});
	},
	
	/**
	 * Starts the measure for the given label.
	 * @param String label
	 */
	start: function(label) {
		this.results[label] = this.results[label] || {};
		this.results[label].start = new Date();
		
		if(this.collective) {
			this.results[label].count = this.results[label].count || 0;
			this.results[label].collected = this.results[label].collected || 0;
		}
	},
	
	/**
	 * Stops the measure for the given label.
	 * Flushes the result into the console if profiler isn't in collective mode.
	 * @param String label
	 */
	stop: function(label) {
		var time = new Date() - this.results[label].start;
		
		if(this.collective) {
			this.results[label].count++;
			this.results[label].collected += time;
		} else {
			console.log(label, time, "ms");
			delete this.results[label];
		}
	}
};

Core.Profiler = new Core.Profiler();